package com.wgts.wgts.mvp.view.activity.pay;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerPayDefeatedComponent;
import com.wgts.wgts.dagger.module.PayResultModule;
import com.wgts.wgts.localbean.PayData;
import com.wgts.wgts.mvp.presenter.IPayResultPresenter;
import com.wgts.wgts.mvp.view.IPayResultView;
import com.wgts.wgts.mvp.view.activity.myorder.MyOrderActivity;
import com.wgts.wgts.mvp.view.activity.myorder.MyOrderDetailsActivity;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.thirdParty.pay.PayTools;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/10/813:12
 *
 */public class PayDefeatedActivity extends BaseActivity implements IPayResultView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.order_number_text)
    TextView orderNumberText;
    @BindView(R.id.watch_order_ly)
    LinearLayout watchOrderLy;
    @BindView(R.id.continue_pay_ly)
    LinearLayout continuePayLy;
    @BindView(R.id.pay_result_recycle)
    RecyclerView payResultRecycle;
    @BindView(R.id.placeholder_text)
    TextView placeholderText;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private String orderId, SneedPayPrice, SplatformCoin, SorderType, orderCode;
    private PayData payData;

    private PayResultRecycleAdapter adapter;
    private List<SearchShopResultEntity.ShopList> shopLists;

    @Inject
    IPayResultPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_defeated_activity);
        ButterKnife.bind(this);
        includeTitle.setText("支付结果");
        Intent intent = getIntent();
        payData = intent.getParcelableExtra(PayTools.PAY_DATE);
        orderId = payData.orderId;
        SneedPayPrice = payData.needPayPrice;
        SplatformCoin = payData.platformCoin;
        SorderType = payData.orderType;
        orderCode = payData.orderCode;
        orderNumberText.setText("订单号：" + orderCode);
        initView();
    }

    @Override
    public void init() {
        DaggerPayDefeatedComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .payResultModule(new PayResultModule(this))
                .build()
                .inject(this);
        shopLists = new ArrayList<>();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        payResultRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PayResultRecycleAdapter(this, shopLists);
        payResultRecycle.setAdapter(adapter);
        //presenter.initList();
        //load();
    }

    @OnClick({R.id.back_btn_ly, R.id.watch_order_ly, R.id.continue_pay_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.watch_order_ly:
                Intent intent1 = new Intent(PayDefeatedActivity.this, MyOrderDetailsActivity.class);
                intent1.putExtra(MyOrderDetailsActivity.ORDER_ID, payData.orderId);
                intent1.putExtra(MyOrderDetailsActivity.PAY_STATUS, "ON_GOING");
                startActivity(intent1);
                finish();
                break;
            case R.id.continue_pay_ly:
                Intent intent = new Intent(PayDefeatedActivity.this, ContinuePayActivity.class);
                intent.putExtra(PayTools.PAY_DATE, payData);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        payData = intent.getParcelableExtra(PayTools.PAY_DATE);
        orderId = payData.orderId;
        SneedPayPrice = payData.needPayPrice;
        SplatformCoin = payData.platformCoin;
        SorderType = payData.orderType;
        orderCode = payData.orderCode;
    }

    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void initList(SearchShopResultEntity entity, boolean noMore) {
        shopLists.clear();
        adapter.setNoMore(noMore);
        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }
        else {
            refreshLayout.setEnableLoadmore(true);
        }
        shopLists.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadMore(SearchShopResultEntity entity, boolean noMore) {
        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }
        else {
            refreshLayout.setEnableLoadmore(true);
        }
        shopLists.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }
        });
    }
}
